package models

import (
	"git.inedcn.com/ined/cmn"
	"rongyy/src/app"
	"rongyy/src/middlewares"
	"strconv"
	"time"
)

type RecruitmentModel struct {
	BaseModel `xorm:"extends"`
	//项目名称
	Name string `xorm:"name" json:"name"`
	//项目编号
	Num string `xorm:"num"  json:"num"`
	//方案号
	SchemeNum string `xorm:"scheme_num"  json:"scheme_num"`
	//申报方
	DeclaringParty string `xorm:"declaring_party"  json:"declaring_party"`
	//实验药物
	Drug     string       `xorm:"drug"  json:"drug"`
	StopTime cmn.JsonDate `xorm:"stop_time"  json:"stop_time"`
	//阶段
	Stage string `xorm:"stage"  json:"stage"`
	//治疗计划
	TreatmentPlan string `xorm:"treatment_plan"  json:"treatment_plan"`
	//入选标准不能为空
	InclusionCriteria string `xorm:"inclusion_criteria"  json:"inclusion_criteria"`
	//排除标准不能为空
	ExclusionCriteria string `xorm:"exclusion_criteria"  json:"exclusion_criteria"`
	//治疗线
	TreatmentLines string `xorm:"treatment_lines"  json:"treatment_lines"`
	//联系人
	Contacts string `xorm:"contacts"  json:"contacts"`
	//联系人电话
	ContactsPhone string `xorm:"contacts_phone"  json:"contacts_phone"`
	//主医生
	MasterDoctor string `xorm:"master_doctor"  json:"master_doctor"`
	//病种
	DiseaseIds string `xorm:"disease_ids"  json:"diseaseIds"`
	//适应症
	IndicationIds string `xorm:"indication_ids"  json:"indicationIds"`
	//靶点
	TargetSpotIds string `xorm:"target_spot_ids"  json:"targetSpotIds"`
	//病理
	PathologyIds string `xorm:"pathology_ids"  json:"pathologyIds"`
	//治疗线
	TreatmentLineIds string `xorm:"treatment_line_ids"  json:"treatmentLineIds"`
	//分期
	StageIds string `xorm:"stage_ids"  json:"stageIds"`
	//研究团队
	ResearchTeams     string               `xorm:"research_teams"  json:"researchTeams"`
	Status            string               `xorm:"status"  json:"status"`
	DiseasesStr       string               `xorm:"-"  json:"diseasesStr"`
	IndicationsStr    string               `xorm:"-"  json:"indicationsStr"`
	PathologysStr     string               `xorm:"-"  json:"pathologysStr"`
	StagesStr         string               `xorm:"-"  json:"stagesStr"`
	TreatmentLinesStr string               `xorm:"-"  json:"treatmentLinesStr"`
	Ids               []int64              `xorm:"-"  json:"ids"`
	Diseases          []DiseaseModel       `xorm:"-"  json:"diseases"`
	Indications       []IndicationModel    `xorm:"-"  json:"indications"`
	TargetSpots       []TargetSpotModel    `xorm:"-"  json:"targetSpots"`
	Pathologies       []PathologyModel     `xorm:"-"  json:"pathologies"`
	Stages            []StageModel         `xorm:"-"  json:"stages"`
	TreatmentLineList []TreatmentLineModel `xorm:"-"  json:"treatmentLineList"`
}

func (sm *RecruitmentModel) TableName() string {
	return "recruitment"
}

func (sm *RecruitmentModel) Insert() error {
	_, err := app.DB().Insert(sm)
	if err != nil {
		return err
	}
	return nil
}

func (sm *RecruitmentModel) SelectByName(name string) (*RecruitmentModel, error) {
	doc := &RecruitmentModel{}
	has, err := app.DB().Where("name = ?", name).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by RecruitmentModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}

func (sm *RecruitmentModel) SelectBySchemeNum(schemeNum string) (*RecruitmentModel, error) {
	doc := &RecruitmentModel{}
	has, err := app.DB().Where("scheme_num =  ?", schemeNum).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by RecruitmentModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}

func (sm *RecruitmentModel) SelectByNum(num string) (*RecruitmentModel, error) {
	doc := &RecruitmentModel{}
	has, err := app.DB().Where("num = ?", num).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by RecruitmentModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}

func (sm *RecruitmentModel) SelectById(id string) (*RecruitmentModel, error) {
	doc := &RecruitmentModel{}
	has, err := app.DB().Where("id = ?", id).Get(doc)
	if err != nil {
		middlewares.Logger.Println("find by RecruitmentModel", err)
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return doc, err
}

func (sm *RecruitmentModel) Update(req *RecruitmentModel) error {
	_, err := app.DB().ID(req.Id).Update(req)
	if err != nil {
		middlewares.Logger.Println("ERROR - type Update failed ", err)
		return err
	}
	return nil
}

func (sm *RecruitmentModel) SelectData(status string, stage string, doctor string, drug string, name string, pathologies string,
	disease string, key string, data []IndicationModel, party string) ([]RecruitmentModel, error) {
	doc := make([]RecruitmentModel, 0)

	s := app.DB().Table("recruitment")

	if status != "" {
		s = s.Where("status like ?", "%"+status+"%")
	}
	if stage != "" {
		s = s.Where("stage_ids like ?", "%"+stage+"%")
	}
	if doctor != "" {
		s = s.Where("master_doctor like ?", "%"+doctor+"%")
	}
	if drug != "" {
		s = s.Where("drug like ?", "%"+drug+"%")
	}
	if name != "" {
		s = s.Where("name like ?", "%"+name+"%")
	}
	if pathologies != "" {
		s = s.Where("pathology_ids like ?", "%"+pathologies+"%")
	}
	if disease != "" {
		s = s.Where("disease_ids like ?", "%"+disease+"%")
	}
	if party != "" {
		s = s.Where("declaring_party like ?", "%"+party+"%")
	}

	if key != "" {
		s = s.Where("drug like ? or name like ?", "%"+key+"%", "%"+key+"%")
	}
	if data != nil && len(data) != 0 {
		for _, v := range data {
			s = s.Where("indication_ids like ?", "%"+strconv.FormatInt(v.Id, 10)+"%")
		}

	}
	s.OrderBy("id desc")

	_, err := s.FindAndCount(&doc)
	if err != nil {
		return nil, err
	}
	return doc, err
}

func (sm *RecruitmentModel) UpdateStatus(req *RecruitmentModel) error {
	_, err := app.DB().ID(req.Id).Cols("status").Update(req)
	if err != nil {
		middlewares.Logger.Println("ERROR - type Update failed ", err)
		return err
	}
	return nil
}

func (sm *RecruitmentModel) Delete(id string) error {
	doc := &RecruitmentModel{}
	_, err := app.DB().Where("id = ?", id).Delete(doc)
	if err != nil {
		middlewares.Logger.Println("delete by RecruitmentModel", err)
		return err
	}
	return nil
}

func (sm *RecruitmentModel) UpdateAllStatus() error {
	format := time.Now().Format("2006-01-02")
	doc := &RecruitmentModel{}
	doc.Status = "招募结束"

	_, err := app.DB().Cols("status").Where("stop_time=?", format).Update(doc)
	if err != nil {
		return err
	}
	return nil
}
